#include "lights.h"

lights::lights()
{
    //set up light
    float position[]={0.0,0.0,1.0,0.0}; //sets a light at (0,0, 1), but with infinite distance (the fourth field)

    float amb[]={0.5,0.5,0.5,1.0};      //sets some ambient light to white

    float spec[]={1.0,1.0,1.0,1.0};     //sets specular light (highly reflective) to white

    float dif[]={1.0,0.0,1.0,0.0};      //sets diffuse light (indirect, bounce light)to purple

    myLights=new lit(amb, spec, dif, position);  //create light with values
}

lights::lights(float position[], float ambient[], float specular[], float diffuse[]){
    float pos[4];
    pos[0]=position[0];
    pos[1]=position[1];
    pos[2]=position[2];
    pos[3]=position[3];

    float amb[4];
    amb[0]=ambient[0];
    amb[1]=ambient[1];
    amb[2]=ambient[2];
    amb[3]=ambient[3];

    float spec[4];
    spec[0]=specular[0];
    spec[1]=specular[1];
    spec[2]=specular[2];
    spec[3]=specular[3];

    float dif[4];
    dif[0]=diffuse[0];
    dif[1]=diffuse[1];
    dif[2]=diffuse[2];
    dif[3]=diffuse[3];

    myLights=new lit(amb, spec, dif, position); //same as constructor but from parameters
}

//deallocate all lights in list
lights::~lights(){
    lit * temp;
    temp=myLights;
    while(temp!=NULL){                        //delete all lights
        temp=myLights->next;
        delete myLights;
        myLights=temp;
    }
}

void lights::turnOnLights(){
    glEnable(GL_LIGHTING);                //activate lighting in world
    lit * temp;
    temp=myLights;
    while(temp!=NULL){
        //change enum? how?
        glEnable(GL_LIGHT0);              //enable each light in list
        glLightfv(GL_LIGHT0, GL_POSITION, temp->position);//set values for each aspect of light (should only need to be done once)
        glLightfv(GL_LIGHT0, GL_AMBIENT, temp->ambient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, temp->diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, temp->specular);
        temp=temp->next;
    }
}

//create a new light in the world
void lights::addLight(float position[], float ambient[], float specular[], float diffuse[]){
    float pos[4];
    pos[0]=position[0];
    pos[1]=position[1];
    pos[2]=position[2];
    pos[3]=position[3];

    float amb[4];
    amb[0]=ambient[0];
    amb[1]=ambient[1];
    amb[2]=ambient[2];
    amb[3]=ambient[3];

    float spec[4];
    spec[0]=specular[0];
    spec[1]=specular[1];
    spec[2]=specular[2];
    spec[3]=specular[3];

    float dif[4];
    dif[0]=diffuse[0];
    dif[1]=diffuse[1];
    dif[2]=diffuse[2];
    dif[3]=diffuse[3];

    lit *temp, *current;
    current=myLights;
    temp=myLights->next;
    while(temp!=NULL){
        current=temp;
        temp=current->next;
    }
    current->next=new lit(amb, spec, dif, position); //add to list of lights
}

void lights::turnOffLights(){
    glDisable(GL_LIGHTING);                          //disales general lighting, not each light
}
